package com.cc.poc.jms;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.widget.BaseAdapter;
import android.widget.ListAdapter;

/**
 * Helper class for providing sample content for user interfaces created by
 * Android template wizards.
 * <p>
 */
public class JmsContent {

    /** Host to connect to. */
    private static final String HOST = "apps.linuxd.org";
    /** Port to connect to. */
    private static final String PORT = "61613";
    /** User for authentication. */
    private static final String USER = "user";
    /** Password for authentication. */
    private static final String PASS = "pass";
    /** Queue/Topic to subscribe to. */
    private static final String QUEUE = "/queue/test";
    /** Create a static task object to use. */
    private static JmsListenerTask task;
    /** Create a static adapter object to use. */
    private static ListAdapter adapter;
    /** An array of items. */
    public static final List<JmsItem> ITEMS = new ArrayList<JmsItem>();
    /** A map of items, by ID. */
    public static final Map<String, JmsItem> ITEM_MAP =
        new HashMap<String, JmsItem>();


	public static void startListener() throws Exception {
		task = new JmsListenerTask();
		task.execute(HOST, PORT, USER, PASS, QUEUE);
	}

	public static void stopListener() throws Exception {
		task.onCancel(QUEUE, true);
	}

	public static void addItem(JmsItem item) {
		ITEMS.add(item);
		ITEM_MAP.put(item.id, item);
		((BaseAdapter) adapter).notifyDataSetChanged();
	}

	public static void removeItem(JmsItem item) {
		ITEM_MAP.remove(item.id);
		ITEMS.remove(item.id);
		((BaseAdapter) adapter).notifyDataSetChanged();
	}

	/**
	 * A dummy item representing a piece of content.
	 */
	public static class JmsItem {
		/** The item id. */
		public String id;
		/** The item content. */
		public String content;

		public JmsItem(String id, String content) {
			this.id = id;
			this.content = content;
		}

		@Override
		public final String toString() {
			return content;
		}
	}

	public static ListAdapter getAdapter() {
		return adapter;
	}

	public static void setAdapter(ListAdapter adapter) {
		JmsContent.adapter = adapter;
	}


}
